function vargout = lpcvd_h2mode_2D(h,mode,method)
% NOTE: assume the h profile is in domain [0,pi)*[0,pi).
assert(size(h,1)==size(h,2),'h must be a square matrix');
L = size(h,1);
dx = pi/L;
x = 0:dx:(pi-dx);
y = 0:dx:(pi-dx);
% z = zeros(mode,mode);

if nargin < 2
    mode = L;
end
if(nargin < 3)
    method = 'sin_cos';
end
% for m = 0:mode-1
%     for n = 0:mode-1
%         for ix = 1:L
%             for iy = 1:L
%                 if(m==0&&n==0)
%                     z(m+1,n+1) = z(m+1,n+1)+h(ix,iy)*dx^2/pi;
%                 elseif(m==0&&n~=0)
%                     z(m+1,n+1) = z(m+1,n+1)+h(ix,iy)*dx*exp(-1i*2*y(iy)*n)*(exp(-1i*2*n*dx)-1)/(-1i*2*pi*n);
%                 elseif(m~=0&&n==0)
%                     z(m+1,n+1) = z(m+1,n+1)+h(ix,iy)*dx*exp(-1i*2*m*x(ix))*(exp(-1i*2*m*dx)-1)/(-1i*2*pi*m);
%                 else
%                     z(m+1,n+1) = z(m+1,n+1)+h(ix,iy)*exp(-1i*2*m*x(ix)-1i*2*n*y(iy))/(-4*pi*m*n)*(exp(-1i*2*m*dx)-1)*(exp(-1i*2*n*dx)-1);
%                 end
%             end
%         end
%     end
% end

% Old code 2010.03.04 ----------------------------------------------------
% if strcmp(method,'fft')
%     z_full = fft2(h)*pi/(L^2);
%     z = z_full(1:mode,1:mode);
% else
%     for m = 0:mode-1
%         for n = 0:mode-1
%             for ix = 1:L
%                 for iy = 1:L
%                     z(m+1,n+1) = z(m+1,n+1)+h(ix,iy)*exp(-2*1i*(m*x(ix)+n*y(iy)));
%                 end
%             end
%             z(m+1,n+1) = z(m+1,n+1)*pi/(L^2);
%         end
%     end
% end
% ------------------------------------------------------------------------
z = zeros(mode+1,2*mode+1);
m0 = 1;
n0 = mode+1;
if strcmp(method,'fft')
else
    for m = 0:mode
        for n = -mode:mode
            for ix = 1:L
                for iy = 1:L
                    z(m+m0,n+n0) = z(m+m0,n+n0)+h(iy,ix)*exp(-2*1i*(m*x(ix)+n*y(iy)));
                end
            end
            z(m+m0,n+n0) = z(m+m0,n+n0)*pi/(L^2);
        end
    end
end
% ------------------------------------------------------------------------